#!/bin/bash

# 定义变量：数据同步日期，表示同步哪一天日志数据
# 第1、执行shell脚本时，传递参数
# 第2、如果没有传递参数，同步前一天数据
if [ -n "$2" ] ; then
  data_date=$2
else
  data_date=`date -d "-1 day" +%F`
fi

# 加载数据语句
ODS_ADS_SQL="
LOAD DATA INPATH '/warehouse/app_ads/ods_ads/${data_date}'
    OVERWRITE INTO TABLE jtp_ads_warehouse.ods_ads
    PARTITION (dt = '${data_date}');
"

ODS_PLATFORM_INFO_SQL="
LOAD DATA INPATH '/warehouse/app_ads/ods_platform_info/${data_date}'
    OVERWRITE INTO TABLE jtp_ads_warehouse.ods_platform_info
    PARTITION (dt = '${data_date}');
"

ODS_PRODUCT_SQL="
LOAD DATA INPATH '/warehouse/app_ads/ods_product/${data_date}'
    OVERWRITE INTO TABLE jtp_ads_warehouse.ods_product
    PARTITION (dt = '${data_date}');
"

ODS_ADS_PLATFORM_SQL="
LOAD DATA INPATH '/warehouse/app_ads/ods_ads_platform/${data_date}'
    OVERWRITE INTO TABLE jtp_ads_warehouse.ods_ads_platform
    PARTITION (dt = '${data_date}');
"

# 执行脚本
case $1 in
  "all")
    /opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "
    ${ODS_ADS_SQL}${ODS_PLATFORM_INFO_SQL}${ODS_PRODUCT_SQL}${ODS_ADS_PLATFORM_SQL}"
    ;;
  "ods_ads")
    /opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${ODS_ADS_SQL}"
  ;;
  "ods_platform_info")
    /opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${ODS_PLATFORM_INFO_SQL}"
  ;;
  "ods_product")
    /opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${ODS_PRODUCT_SQL}"
  ;;
  "ods_ads_platform")
    /opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${ODS_ADS_PLATFORM_SQL}"
  ;;
  *)
    echo "Usage: $0 {all|ods_ads|ods_platform_info|ods_product|ods_ads_platform} [date]"
    echo "Example: $0 all 2025-07-02"
    exit 1
  ;;
esac

# ========================================================================
# 执行脚本时，传递参数：sh jtp_ads_hdfs_to_ods.sh all 2025-07-02
# ========================================================================